import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:loading/indicator/line_scale_pulse_out_indicator.dart';
import 'package:loading/loading.dart';

//四种视图状态
enum LoadStatus { success, error, loading, empty }

class LoadingContainer extends StatefulWidget {
  final LoadStatus status; //页面状态
  final Widget? child; //成功视图
  final VoidCallback? onReload; // 重试

  const LoadingContainer({
    Key? key,
    this.status = LoadStatus.loading, //默认为加载状态
    this.child,
    this.onReload,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _LoadStateContainerState();
  }
}

class _LoadStateContainerState extends State<LoadingContainer> {
  @override
  Widget build(BuildContext context) {
    return SizedBox(
      //宽高都充满屏幕剩余空间
      width: double.infinity,
      height: double.infinity,
      child: _buildWidget,
    );
  }

  ///根据不同状态来显示不同的视图
  Widget? get _buildWidget {
    switch (widget.status) {
      case LoadStatus.success:
        return widget.child;
      case LoadStatus.error:
        return _errorView;
      case LoadStatus.loading:
        return _loadingView;
      case LoadStatus.empty:
        return NoDataView(
          onReload: widget.onReload,
        );
      default:
        return null;
    }
  }

  ///加载中视图
  Widget get _loadingView {
    return Container(
      width: double.infinity,
      height: double.infinity,
      alignment: Alignment.center,
      color: Colors.white,
      child: Loading(
        indicator: LineScalePulseOutIndicator(),
        size: 36.0,
        color: Colors.blueAccent,
      ),
    );
  }

  ///错误视图
  Widget get _errorView {
    return Container(
        width: double.infinity,
        height: double.infinity,
        color: Colors.white,
        child: InkWell(
          onTap: widget.onReload,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              SizedBox(
                width: Get.width / 3,
                height: Get.width / 3,
                child: Image.asset('images/load_error.png'),
              ),
              const Text(
                "加载失败，请轻触重试!",
                style: TextStyle(color: Colors.black38, fontSize: 14),
              ),
            ],
          ),
        ));
  }
}

class NoDataView extends StatefulWidget {
  final VoidCallback? onReload; //无数据事件处理

  const NoDataView({Key? key, this.onReload}) : super(key: key);

  @override
  _NoDataViewState createState() => _NoDataViewState();
}

class _NoDataViewState extends State<NoDataView> {
  @override
  Widget build(BuildContext context) {
    return Container(
        color: Colors.white,
        width: 750,
        height: double.infinity,
        child: InkWell(
          onTap: widget.onReload,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              SizedBox(
                width: Get.width / 3,
                height: Get.width / 4,
                child: Image.asset('images/no_data.png'),
              ),
              const Text(
                '暂无相关数据,轻触重试~',
                style: TextStyle(color: Colors.black38, fontSize: 14),
              )
            ],
          ),
        ));
  }
}
